home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / src / print_cmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-22  |  12.6 KB  |  575 lines

  1. /* print_command -- A way to make readable commands from a command tree. */
  2.  
  3. /* Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22.  
  23. #if defined (HAVE_VPRINTF)
  24. #include <varargs.h>
  25. #endif
  26.  
  27. #include "shell.h"
  28. #include "y.tab.h"
  29.  
  30. static int indentation = 0;
  31. static int indentation_amount = 4;
  32.  
  33. #define PRINTED_COMMAND_GROW_SIZE 1024
  34. char *the_printed_command = (char *)NULL;
  35. int the_printed_command_size = 0;
  36. int command_string_index = 0;
  37.  
  38. /* Non-zero means the stuff being printed is inside of a function def. */
  39. static int inside_function_def = 0;
  40. static int skip_this_indent = 0;
  41.  
  42. /* Print COMMAND (a command tree) on standard output. */
  43. print_command (command)
  44.      COMMAND *command;
  45. {
  46.   char *make_command_string ();
  47.   command_string_index = 0;
  48.   printf ("%s", make_command_string (command));
  49. }
  50.  
  51. /* Make a string which is the printed representation of the command
  52.    tree in COMMAND.  We return this string.  However, the string is
  53.    not consed, so you have to do that yourself if you want it to
  54.    remain around. */
  55. char *
  56. make_command_string (command)
  57.      COMMAND *command;
  58. {
  59.   command_string_index = 0;
  60.   make_command_string_internal (command);
  61.   return (the_printed_command);
  62. }
  63.  
  64. /* The internal function.  This is the real workhorse. */
  65. make_command_string_internal (command)
  66.      COMMAND *command;
  67. {
  68.   if (!command)
  69.     {
  70.       cprintf ("");
  71.     }
  72.   else
  73.     {
  74.       if (skip_this_indent)
  75.     skip_this_indent--;
  76.       else
  77.     indent (indentation);
  78.  
  79.       if (command->subshell == WANT_SUBSHELL)
  80.     cprintf ("( ");
  81.  
  82.       if (command->invert_pipeline)
  83.     cprintf (" ! ");
  84.  
  85.       switch (command->type)
  86.     {
  87.     case cm_for:
  88.       print_for_command (command->value.For);
  89.       break;
  90.  
  91.     case cm_case:
  92.       print_case_command (command->value.Case);
  93.       break;
  94.  
  95.     case cm_while:
  96.       print_while_command (command->value.While);
  97.       break;
  98.  
  99.     case cm_until:
  100.       print_until_command (command->value.While);
  101.       break;
  102.  
  103.     case cm_if:
  104.       print_if_command (command->value.If);
  105.       break;
  106.  
  107.     case cm_simple:
  108.       print_simple_command (command->value.Simple);
  109.       break;
  110.  
  111.     case cm_connection: 
  112.  
  113.       skip_this_indent++;
  114.       make_command_string_internal (command->value.Connection->first);
  115.  
  116.       switch (command->value.Connection->connector)
  117.         {
  118.         case '&':
  119.         case '|':
  120.           {
  121.         char c = command->value.Connection->connector;
  122.         cprintf (" %c", c);
  123.         if (c != '&' || command->value.Connection->second)
  124.           {
  125.             cprintf (" ");
  126.             skip_this_indent++;
  127.           }
  128.           }
  129.           break;
  130.  
  131.         case AND_AND:
  132.           cprintf (" && ");
  133.           if (command->value.Connection->second)
  134.         skip_this_indent++;
  135.           break;
  136.  
  137.         case OR_OR:
  138.           cprintf (" || ");
  139.           if (command->value.Connection->second)
  140.         skip_this_indent++;
  141.           break;
  142.     
  143.         case ';':
  144.           cprintf (";");
  145.  
  146.           if (inside_function_def)
  147.         cprintf ("\n");
  148.           else
  149.         {
  150.           cprintf (" ");
  151.           if (command->value.Connection->second)
  152.             skip_this_indent++;
  153.         }
  154.           break;
  155.  
  156.         default:
  157.           cprintf ("OOPS!  Bad connector `%d'!",
  158.                command->value.Connection->connector);
  159.           break;
  160.         }
  161.  
  162.       make_command_string_internal (command->value.Connection->second);
  163.       break;
  164.       
  165.     case cm_function_def:
  166.       print_function_def (command->value.Function_def);
  167.       break;
  168.  
  169.     case cm_group:
  170.       print_group_command (command->value.Group);
  171.       break;
  172.  
  173.     default:
  174.       programming_error ("OOPS!  Bad command type `%d'!", command->type);
  175.       break;
  176.     }
  177.  
  178.       if (command->subshell == WANT_SUBSHELL)
  179.     cprintf (" )");
  180.  
  181.       if (command->redirects)
  182.     print_redirection_list (command->redirects);
  183.     }
  184. }
  185.  
  186. print_word_list (list, separator)
  187.      WORD_LIST *list;
  188.      char *separator;
  189. {
  190.   while (list)
  191.     {
  192.       printf ("%s", list->word->word);
  193.       list = list->next;
  194.       if (list)
  195.     printf ("%s", separator);
  196.     }
  197. }
  198.  
  199. command_print_word_list (list, separator)
  200.      WORD_LIST *list;
  201.      char *separator;
  202. {
  203.   while (list)
  204.     {
  205.       cprintf ("%s", list->word->word);
  206.       list = list->next;
  207.       if (list)
  208.     cprintf ("%s", separator);
  209.     }
  210. }
  211.  
  212. print_for_command (for_command)
  213.      FOR_COM *for_command;
  214. {
  215.   cprintf ("for %s in ", for_command->name->word);
  216.   command_print_word_list (for_command->map_list, " ");
  217.   cprintf (";");
  218.   newline ("do\n");
  219.   indentation += indentation_amount;
  220.   make_command_string_internal (for_command->action);
  221.   cprintf (";");
  222.   indentation -= indentation_amount;
  223.   newline ("done");
  224. }
  225.  
  226. print_group_command (group_command)
  227.      GROUP_COM *group_command;
  228. {
  229.   cprintf ("{ ");
  230.  
  231.   if (!inside_function_def)
  232.     skip_this_indent++;
  233.   else
  234.     cprintf ("\n");
  235.  
  236.   make_command_string_internal (group_command->command);
  237.  
  238.   if (inside_function_def)
  239. #if 0
  240.     cprintf ("; \n}");
  241. #else
  242.     cprintf ("\n}");
  243. #endif
  244.   else
  245.     cprintf (" }");
  246. }
  247.  
  248. print_case_command (case_command)
  249.      CASE_COM *case_command;
  250. {
  251.   cprintf ("case %s in ", case_command->word->word);
  252.   if (case_command->clauses)
  253.     print_case_clauses (case_command->clauses);
  254.   newline ("esac");
  255. }
  256.  
  257. print_case_clauses (clauses)
  258.      PATTERN_LIST *clauses;
  259. {
  260.   indentation += indentation_amount;
  261.   while (clauses)
  262.     {
  263.       newline ("");
  264.       command_print_word_list (clauses->patterns, " | ");
  265.       cprintf (")\n");
  266.       indentation += indentation_amount;
  267.       make_command_string_internal (clauses->action);
  268.       indentation -= indentation_amount;
  269.       newline (";;");
  270.       clauses = clauses->next;
  271.     }
  272.   indentation -= indentation_amount;
  273. }
  274.  
  275. print_while_command (while_command)
  276.      WHILE_COM *while_command;
  277. {
  278.   print_until_or_while (while_command, "while");
  279. }
  280.  
  281. print_until_command (while_command)
  282.      WHILE_COM *while_command;
  283. {
  284.   print_until_or_while (while_command, "until");
  285. }
  286.  
  287. print_until_or_while (while_command, which)
  288.      WHILE_COM *while_command;
  289.      char *which;
  290. {
  291.   cprintf ("%s ", which);
  292.   skip_this_indent++;
  293.   make_command_string_internal (while_command->test);
  294.   cprintf (";");
  295.   newline ("do\n");
  296.   indentation += indentation_amount;
  297.   make_command_string_internal (while_command->action);
  298.   indentation -= indentation_amount;
  299.   cprintf (";");
  300.   newline ("done");
  301. }
  302.  
  303. print_if_command (if_command)
  304.      IF_COM *if_command;
  305. {
  306.   cprintf ("if ");
  307.   skip_this_indent++;
  308.   make_command_string_internal (if_command->test);
  309.   cprintf (" ; then\n");
  310.   indentation += indentation_amount;
  311.   make_command_string_internal (if_command->true_case);
  312.   indentation -= indentation_amount;
  313.  
  314.   if (if_command->false_case)
  315.     {
  316.       cprintf (";");
  317.       newline ("else\n");
  318.       indentation += indentation_amount;
  319.       make_command_string_internal (if_command->false_case);
  320.       indentation -= indentation_amount;
  321.     }
  322.   cprintf (" ;");
  323.   newline ("fi");
  324. }
  325.  
  326. print_simple_command (simple_command)
  327.      SIMPLE_COM *simple_command;
  328. {
  329.   command_print_word_list (simple_command->words, " ");
  330.   if (simple_command->redirects)
  331.     {
  332.       cprintf (" ");
  333.       print_redirection_list (simple_command->redirects);
  334.     }
  335. }
  336.  
  337. print_redirection_list (redirects)
  338.      REDIRECT *redirects;
  339. {
  340.   while (redirects)
  341.     {
  342.       print_redirection (redirects);
  343.       cprintf (" ");
  344.       redirects = redirects->next;
  345.     }
  346. }
  347.  
  348. print_redirection (redirect)
  349.      REDIRECT *redirect;
  350. {
  351.   int kill_leading = 0;
  352.   int redirector = redirect->redirector;
  353.   WORD_DESC *redirectee = redirect->redirectee.filename;
  354.  
  355.   switch (redirect->instruction)
  356.     {
  357.     case r_output_direction:
  358.       if (redirector != 1)
  359.     cprintf ("%d", redirector);
  360.       cprintf (">%s", redirectee->word);
  361.       break;
  362.  
  363.     case r_input_direction:
  364.       if (redirector != 0)
  365.     cprintf ("%d", redirector);
  366.       cprintf ("<%s", redirectee->word);
  367.       break;
  368.  
  369.     case r_inputa_direction:    /* Redirection created by the sh. */
  370.       cprintf ("&");
  371.       break;
  372.  
  373.     case r_appending_to:
  374.       if (redirector != 1)
  375.     cprintf ("%d", redirector);
  376.       cprintf (">>%s", redirectee->word);
  377.       break;
  378.  
  379.     case r_deblank_reading_until:
  380.       kill_leading++;
  381.       /* ... */
  382.     case r_reading_until:
  383.       if (redirector != 0)
  384.     cprintf ("%d", redirector);
  385.       cprintf ("<<%s%s", kill_leading? "-" : "", redirect->here_doc_eof);
  386.       /* This is only important if we are printing a function definition
  387.      for export.  If not, we could overrun the buffer that `cprintf'
  388.      writes into, quite unexpectedly -- when unpacking shar files, for
  389.      instance. */
  390.       if (inside_function_def)
  391.     {
  392.       cprintf ("\n");
  393.       cprintf ("%s%s", redirect->redirectee.filename->word,
  394.                redirect->here_doc_eof);
  395.     }
  396.       break;
  397.  
  398.     case r_duplicating:
  399.       cprintf ("%d>&%d", redirector, (int)redirectee);
  400.       break;
  401.  
  402.     case r_close_this:
  403.       cprintf ("%d>&-", redirector);
  404.       break;
  405.  
  406.     case r_err_and_out:
  407.       cprintf (">&%s", redirectee->word);
  408.       break;
  409.  
  410.     case r_input_output:
  411.       if (redirector != 1)
  412.     cprintf ("%d", redirector);
  413.       cprintf ("<>%s", redirectee->word);
  414.       break;
  415.  
  416.     case r_output_force:
  417.       if (redirector != 1)
  418.     cprintf ("%d", redirector);
  419.       cprintf (">|%s", redirectee->word);
  420.       break;
  421.     }
  422. }
  423.  
  424. static void
  425. reset_locals ()
  426. {
  427.   inside_function_def = 0;
  428.   indentation = 0;
  429. }
  430.  
  431. print_function_def (func)
  432.      FUNCTION_DEF *func;
  433. {
  434.   cprintf ("function %s () \n", func->name->word);
  435.   add_unwind_protect (reset_locals, 0);
  436.   inside_function_def++;
  437.   skip_this_indent++;
  438.   indentation += indentation_amount;
  439.   make_command_string_internal (func->command);
  440.   remove_unwind_protect ();
  441.   indentation -= indentation_amount;
  442.   inside_function_def--;
  443. }
  444.  
  445. /* Return the string representation of the named function.
  446.    NAME is the name of the function.
  447.    COMMAND is the function body.  It should be a GROUP_COM.
  448.    MULTI_LINE is non-zero to pretty-print, or zero for all on one line.
  449.   */
  450. char *
  451. named_function_string (name, command, multi_line)
  452.      char *name;
  453.      COMMAND *command;
  454.      int multi_line;
  455. {
  456.   char *result;
  457.   int old_indent = indentation, old_amount = indentation_amount;
  458.  
  459.   command_string_index = 0;
  460.  
  461.   if (name && *name)
  462.     cprintf ("%s ", name);
  463.  
  464.   cprintf ("() ");
  465.  
  466.   if (!multi_line)
  467.     {
  468.       indentation = 1;
  469.       indentation_amount = 0;
  470.     }
  471.   else
  472.     {
  473.       cprintf ("\n");
  474.       indentation += indentation_amount;
  475.     }
  476.  
  477.   inside_function_def++;
  478.   skip_this_indent++;
  479.   make_command_string_internal (command);
  480.  
  481.   indentation = old_indent;
  482.   indentation_amount = old_amount;
  483.   inside_function_def--;
  484.  
  485.   result = the_printed_command;
  486.  
  487.   if (!multi_line)
  488.     {
  489. #if 0
  490.       register int i;
  491.       for (i = 0; result[i]; i++)
  492.     if (result[i] == '\n')
  493.       {
  494.         strcpy (&result[i], &result[i + 1]);
  495.         --i;
  496.       }
  497. #else
  498.       if (result[2] == '\n')    /* XXX -- experimental */
  499.         strcpy (&result[2], &result[3]);
  500. #endif
  501.     }
  502.  
  503.   return (result);
  504. }
  505.  
  506. newline (string)
  507.      char *string;
  508. {
  509.   cprintf ("\n");
  510.   indent (indentation);
  511.   cprintf ("%s", string);
  512. }
  513.  
  514. indent (amount)
  515.      int amount;
  516. {
  517.   while (amount-- > 0)
  518.     cprintf (" ");
  519. }
  520.  
  521. #if !defined (HAVE_VPRINTF)
  522. /* How to make the string. */
  523. cprintf (control, arg1, arg2, arg3, arg4, arg5)
  524.      char *control;
  525. {
  526.   char temp_buffer[5000];
  527.   int l;
  528.  
  529.   sprintf (temp_buffer, control, arg1, arg2, arg3, arg4, arg5);
  530.   l = strlen (temp_buffer);
  531.  
  532.   if (!the_printed_command)
  533.     the_printed_command =
  534.       (char *)xmalloc (the_printed_command_size = PRINTED_COMMAND_GROW_SIZE);
  535.  
  536.   while (the_printed_command_size <= command_string_index + l)
  537.     the_printed_command = (char *)
  538.       xrealloc (the_printed_command,
  539.         the_printed_command_size += PRINTED_COMMAND_GROW_SIZE);
  540.  
  541.   strcpy (the_printed_command + command_string_index, temp_buffer);
  542.   command_string_index += l;
  543. }
  544.  
  545. #else /* We have support for varargs. */
  546.  
  547. /* How to make the string. */
  548. cprintf (va_alist)
  549.      va_dcl
  550. {
  551.   char temp_buffer[5000];
  552.   int l;
  553.   char *control;
  554.   va_list args;
  555.  
  556.   va_start (args);
  557.   control = va_arg (args, char *);
  558.   vsprintf (temp_buffer, control, args);
  559.   va_end (args);
  560.   l = strlen (temp_buffer);
  561.  
  562.   if (!the_printed_command)
  563.     the_printed_command =
  564.       (char *)xmalloc (the_printed_command_size = PRINTED_COMMAND_GROW_SIZE);
  565.  
  566.   while (the_printed_command_size <= command_string_index + l)
  567.     the_printed_command = (char *)
  568.       xrealloc (the_printed_command,
  569.         the_printed_command_size += PRINTED_COMMAND_GROW_SIZE);
  570.  
  571.   strcpy (the_printed_command + command_string_index, temp_buffer);
  572.   command_string_index += l;
  573. }
  574. #endif /* HAVE_VPRINTF */
  575.